package server;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import db.DBmanager;

/**
 * Servlet implementation class startNewGame
 */
public class startNewGame extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public startNewGame() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter out=response.getWriter();
		String numOfPlayers="";
		String initMoney="";
		String userName="";
		try{
			numOfPlayers=request.getParameterValues("numOfPlayers")[0];
			initMoney=request.getParameterValues("initMoney")[0];
			userName=request.getParameterValues("userName")[0];
			}catch(Exception e){
				response.sendError(400, "missing parameters");
				return;
			}
		
		try {
			String res=DBmanager.getDB().openNewGame(numOfPlayers,initMoney,userName);
			if(res.equals("OK")){
				response.setStatus(200);
			}else{
				out.write(res);
				response.setStatus(400);
			}
			DBmanager.getDB().printDB();
		} catch (SQLException e) {
			out.write("Technical problem initiating the game, plz try again");
			response.setStatus(400);
			e.printStackTrace();
		}
		//return 
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//numofplayers
	}

}
